package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.RequestPage;
import io.renren.common.validator.Assert;
import io.renren.entity.JxComputyDeviceEntity;
import io.renren.entity.JxStudentScheduleEntity;
import io.renren.iot.webSocket.util.SendMsg;
import io.renren.iot.webSocket.webService.WebSocketService;
import io.renren.service.JxComputyDeviceService;
import io.renren.service.JxStudentScheduleService;
import io.renren.utils.DateTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 学生实验日程
 */
@RestController
@RequestMapping("/api/studentSchedule")
@Api(tags="实验日程")
public class ApiJxStudentScheduleController extends  BaseController{
    @Autowired
    private JxStudentScheduleService jxStudentScheduleService;
    /**
     * 列表
     */
    @Autowired
    private JxComputyDeviceService  jxComputyDeviceService;

    @Login
    @PostMapping("list")
    @ApiOperation("实验日程")
    public R list(@RequestBody RequestPage page, HttpServletRequest request, HttpServletResponse response){
        Assert.isNull(page, "分页数和每页显示不能为空!");
        Map<String,Object> params=new HashMap<String,Object>();
        params.put("limit",page.getLimit());
        params.put("page",page.getPage());
        params.put("studentId",getUserId(request));
        PageUtils pu = jxStudentScheduleService.getAllWithPage(params);
        return R.ok().put("page",pu);
    }

    @Login
    @PostMapping("open")
    @ApiOperation("实验日程开启")
    public R open(@RequestBody JxStudentScheduleEntity entity, HttpServletRequest request, HttpServletResponse response){
         if((entity.getStudentId()-(Long.parseLong(getUserId(request).toString())))==0){
             Date begainTime=entity.getBegainTime();  // 开始 时间
             Date  endTime=entity.getEndTime();   //  结束时间
             Long id=entity.getId();
             Map<String,Object> params=new HashMap<String,Object>();
             params.put("id",id);
             params.put("studentId",entity.getStudentId());
             JxStudentScheduleEntity en=jxStudentScheduleService.getList(params).get(0);
             if(null==en||!DateTimeUtil.timeEquery(begainTime,en.getBegainTime())||!DateTimeUtil.timeEquery(endTime,en.getEndTime())){
                 return R.error("你未经过身份验证");
             }
             if(DateTimeUtil.compareTime(en.getEndTime(),new Date())){
                 if(DateTimeUtil.compareTime(begainTime,new Date())){
                     return R.error("课程未开启！");
                 }else{
                   return R.ok().put("url",getUrl(request,en.getUseDeviceId(),en.getId()));
                 }
             }else{
                 return R.error("您已经过了有效期！");
             }

         }
        return R.error();
    }

    public  String getUrl(HttpServletRequest request,Long deviceId,Long scheduleId){
        Long timeStemp=new Date().getTime()+1000*60;
        String URL=request.getContextPath()+"/serv/index"+"?key="+timeStemp+"a"+getUserId(request)+"a"+deviceId+"a"+scheduleId;
        return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+URL;
    }

    /**
     * 关闭单个机器
     * @param entity
     * @param request
     * @return
     */
    @Login
    @PostMapping("close")
    @ApiOperation("实验日程开启")
    public R close(@RequestBody JxStudentScheduleEntity entity, HttpServletRequest request){
        Date  endTime=entity.getEndTime(); //  结束时间
        entity=jxStudentScheduleService.getById(entity.getId());
        JxComputyDeviceEntity device=jxComputyDeviceService.getById(entity.getUseDeviceId());
      if(DateTimeUtil.compareTime(endTime,new Date())){  //  未结束
        entity.setStudyStatus("3");   // 暂停
      }else{
          entity.setStudyStatus("2");   // 完成
      }
      device.setUseStatus("0");  //     停止使用
      jxComputyDeviceService.saveOrUpdate(device);
      jxStudentScheduleService.saveOrUpdate(entity);
        return R.ok();
    }

    /**
     * 关闭所有的机器
     */
    @PostMapping("closeAll")
    public  R closeAllDevice(HttpServletRequest request){
        SendMsg  msg=new SendMsg();
        msg.setViewFlag("closeAll");
        WebSocketService.SendMsg(msg);
        return  R.ok();
    }

}
